<?php
    
    include 'phpfunctions.php';

    $book = $_POST['book'];
    $author = $_POST['author'];
    $issn = $_POST['issn'];
    $edition = $_POST['edition'];
    $class = $_POST['class'];
    $professor = $_POST['professor'];
    $price = $_POST['price'];
    $description = $_POST['description'];

    if($issn == "") {
        $issn = "NOT PROVIDED";
    }

    if($edition == "") {
        $edition = "NOT PROVIDED";
    }

    if($professor == "") {
        $professor = "NOT PROVIDED";
    }

    $num_files = glob("books/" . $book . "*.php");

    $ourFileName = "books/" . $book . count($num_files) .".php";
    
    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
    
    $data = '<?php

    // Name: Justin Lee and Alexander Cam Liu
    // Modded code from cas-demo.php by Russell Tokuyama
    include \'../phpfunctions.php\';  // include Russell\'s php header code
    
    if (isset($_SESSION[\'name\'])) {
        ?>
        <!DOCTYPE html>
        <html>
      
        <head>
            <title>' .$book . '</title>
         <!-- Bootstrap -->
            <link href="../css/bootstrap.css" rel="stylesheet">
            <script src="http://code.jquery.com/jquery-latest.js"></script>
            <script src="../js/bootstrap.min.js"></script>
            <link href="../css/book.css" rel="stylesheet">
            <link href="../css/docs.css" rel="stylesheet">
            <script src="../js/infogenerate.js"></script>
        </head>
        <body style="background-color:#FAFAFA">
            <div class="navbar navbar-fixed-top">
              <div class="navbar-inner">
                <div class="container-fluid">
                  <a class="brand" href="#"><?php echo $name ?></a>
                  <div class="nav-collapse collapse">
                    <ul class="nav">
                      <li><a href="<?php echo $logout; ?>">Logout</a></li>
                    </ul>
                  </div>
                </div>
              </div>
            </div>
            <br>
            <div class="container">
                <center><h1>' . $book . ' </h1></center>
                <div class="row">
                    <div class="span6 offset3 block">
                        <figure>
                            <img src="../img/image_not_available.jpg" height="100" width="100" alt="Image not Available" 
                            class="img-polaroid">
                        </figure>
                        <strong>Author: </strong>' . $author . '<br>
                        <strong>Edition: </strong>' . $edition . ' <br>
                        <strong>ISSN: </strong>' . $issn . ' <br>
                        <strong>Class: </strong>' . $class . ' <br>
                        <strong>Professor: </strong>' . $professor . ' <br>
                        <strong>PRICE: $</strong>' . $price . ' <br>
                    </div>
                </div>
                <br>
                 <div class="row">
                    <div class="span6 offset3 block center-text">
                        <center><strong>Name: </strong>' . $name . '</center>
                        <center><strong>Email: </strong>' .$uid . '@hawaii.edu</center>
                    </div>
                </div>
                <br>
                <div class="row">
                    <div class="span2 offset5">
                        <button class="btn btn-large btn-block btn-primary" 
                        type="button" value="LOGIN" onclick="location.href=\'http://localhost\'">Search Again</button>
                    </div>
                </div>
            </div>
            <br>
        </body>
        </html>
    <?php
        } else {
            header ("location: $login");
        }
    ?>
    ';

    fwrite($ourFileHandle, $data);
    fclose($ourFileHandle);
    header("Location: $ourFileName");
?>